local skel = fk.CreateSkill {
  name = "emo__longqi",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__longqi"] = "龙契",
  [":emo__longqi"] = "锁定技。你不能使用手牌中的【杀】，你的【杀】视为随机其他非装备牌。",

  ["@@emo__longqi-inhand"] = "龙契",
}

--- 检测手牌中是否含有杀
---@param player ServerPlayer
local function triggerable(player)
  for _, id in ipairs(player.player_cards[Player.Hand]) do
    if Fk:getCardById(id).trueName == "slash" then
      return true
    end
  end
  return false
end

--- 把手牌的杀改变
---@param player ServerPlayer
local function on_use(player)
  -- 只有本尊才有特殊变化
  local isOwner = player.general == "moesp__longyan" or player.deputyGeneral == "moesp__longyan"
  local special = {"lq_come", "lq_dye", "lq_yoke"}
  local all = table.filter(Fk:getAllCardNames("btd"), function (name)
    return not name:endsWith("slash")
  end)
  local rat = 0.3 -- 特殊卡出现率
  for _, id in ipairs(player:getCardIds("h")) do
    local c = Fk:getCardById(id)
    if c.trueName == "slash" then
      local name
      if isOwner and math.random() < rat then
        name = table.random(special)
      else
        name = table.random(all)
      end
      if name then
        player.room:setCardMark(c, "@@emo__longqi-inhand", name)
      end
    end
  end
  player:filterHandcards()
end

skel:addEffect(fk.GameStart, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and triggerable(player)
  end,
  on_use = function (self, event, target, player, data)
    on_use(player)
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and triggerable(player)
    -- 下面这坨手牌区进出检测好像没必要
    --[[
    for _, move in ipairs(data) do
      if (move.to == player and move.toArea == Card.PlayerHand) or
      (move.from == player and table.find(move.moveInfo, function (info) return info.fromArea == Card.PlayerHand end)) then
        return true
      end
    end
    --]]
  end,
  on_use = function (self, event, target, player, data)
    on_use(player)
  end,
})

--- 防止转化手牌变为杀，例如丈八
skel:addEffect("prohibit", {
  prohibit_use = function (self, player, card)
    if not player:hasSkill(skel.name) then return false end
    local subcards = Card:getIdList(card)
    local hand = player:getCardIds("h")
    return card.trueName == "slash" and #subcards > 0 and
    table.find(subcards, function (id) return table.contains(hand, id) end) ~= nil
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  for _, id in ipairs(player:getCardIds("h")) do
    local c = Fk:getCardById(id)
    if c:getMark("@@emo__longqi-inhand") ~= 0 then
      player.room:setCardMark(c, "@@emo__longqi-inhand", 0)
    end
  end
  player:filterHandcards()
end)

skel:addAcquireEffect(function (self, player, is_start)
  if not is_start then
    on_use(player)
  end
end)

skel:addEffect("filter", {
  mute = true,
  card_filter = function (self, card, player, isJudgeEvent)
    return card and card:getMark("@@emo__longqi-inhand") ~= 0
  end,
  view_as = function (self, player, card)
    local c = Fk:cloneCard(card:getMark("@@emo__longqi-inhand"), card.suit, card.number)
    c.skillName = skel.name
    return c
  end,
})

return skel
